package 月度_季度_年度数据;

import Moudle.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class ParseData {
    static Item[] parseJsonStrToItem(String jsonStr) {
        //把json数据转换为Item类型
        JSONArray json = (JSONArray)JSON.parseArray(jsonStr);
        int size = json.size();
        Item[] items = new Item[size];
        for (int i = 0; i < size; i++) {
            JSONObject jsonObject =(JSONObject) json.get(i);
            String dbcode = jsonObject.getString("dbcode");
            String id = jsonObject.getString("id");
            String isParent = jsonObject.getString("isParent");
            String name = jsonObject.getString("name");
            String pid = jsonObject.getString("pid");
            String wdcode = jsonObject.getString("wdcode");
            items[i] = new Item(dbcode, id, isParent, name, pid, wdcode);
        }
        return items;
    }

    static Excel_class[] parseData(String jsonStr){
        //解析表格数据 json转换为Excel_class
        JSONObject return_data = JSONObject.parseObject(jsonStr).getJSONObject("returndata");

        JSONArray wd_nodes = return_data.getJSONArray("wdnodes");
        //行 时间
        JSONArray timeArray = wd_nodes.getJSONObject(1).getJSONArray("nodes");
        //列
        JSONArray zbArray = wd_nodes.getJSONObject(0).getJSONArray("nodes");
        //数据
        JSONArray dataArray = return_data.getJSONArray("datanodes");

        //data 解析  组成map
        Map<String, DataNode> dataNodeMap = new HashMap<>();
        int datanodes_len = dataArray.size();
        for (int i = 0; i < datanodes_len; i++){
            JSONObject jsonObject = dataArray.getJSONObject(i);
            DataNode dataNode = new DataNode(jsonObject.getString("code"),
                                             jsonObject.getJSONObject("data").getString("hasdata"),
                                             jsonObject.getJSONObject("data").getString("data"));
            dataNodeMap.put(dataNode.code, dataNode);
        }

        //列 解析
        int zbArray_len = zbArray.size();
        ZB_Class[] zb_classes = new ZB_Class[zbArray_len];
        for (int i = 0; i < zbArray_len; i++) {
            JSONObject jsonObject = zbArray.getJSONObject(i);
            ZB_Class zb = new ZB_Class(jsonObject.getString("cname"),
                                       jsonObject.getString("code"),
                                       jsonObject.getString("memo"),
                                       jsonObject.getString("unit"));
            zb_classes[i] = zb;
        }

        //行 解析
        int timeArray_len = timeArray.size();
        Time_Class[] time_classes = new Time_Class[timeArray_len];
        for (int i = 0; i < timeArray_len; i++) {
            JSONObject jsonObject = timeArray.getJSONObject(i);
            Time_Class time_class = new Time_Class(jsonObject.getString("cname"),
                                                   jsonObject.getString("code"));
            time_classes[i] = time_class;
        }

        //写入excel每行的数据
        Excel_class[] excel_classes = new Excel_class[timeArray_len * zbArray_len + 1];
        int index = 0;
        String 注 = "注：";
        for(ZB_Class zb_class : zb_classes){
            if (!注.contains(zb_class.memo)){
                注 = 注 + zb_class.memo + ";";
            }
            for(Time_Class time_class : time_classes){
                String code = "zb."+ zb_class.code + "_sj." + time_class.code;
                DataNode data = dataNodeMap.get(code);
                String 指标 = zb_class.cname;
                if (!zb_class.unit.equals("")){
                    指标 = 指标 + "(" + zb_class.unit + ")";
                }
                String 时间 = time_class.cname;
                String 数值 = data.hasData.equals("true") ? data.data : "";
                if (!数值.equals("")){
                    double v = Double.parseDouble(数值);
                    DecimalFormat df = new DecimalFormat("0.00");
                    数值 = String.valueOf(df.format(v));
                }
                Excel_class excel_class = new Excel_class(指标, 时间, 数值);
                excel_classes[index++] = excel_class;
            }
        }

        //ecxel_classes[last] 存放 注释 信息
        excel_classes[excel_classes.length-1] = new Excel_class(注,"", "");

        return excel_classes;
    }


}
